package cn.fyl.pet.controller;

import cn.fyl.basic.RocketMq.RocketMQProducer;
import cn.fyl.basic.exception.BusinessException;
import cn.fyl.basic.util.LoginContext;
import cn.fyl.pet.dto.OrderDto;
import cn.fyl.pet.service.ISearchMasterMsgService;
import cn.fyl.pet.domain.SearchMasterMsg;
import cn.fyl.pet.query.SearchMasterMsgQuery;
import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.user.domain.LoginInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;



    /**
     * 接口：添加或修改
     * @param searchMasterMsg  传递的实体
     * @return JsonResult 响应给前端
     */
    @PostMapping
    public JsonResult save(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return JsonResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public JsonResult remove(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return JsonResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
                searchMasterMsgService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public SearchMasterMsg findById(@PathVariable("id")Long id) {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<SearchMasterMsg> findAll(){
        return searchMasterMsgService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PutMapping
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.queryData(query);
    }

    //发布寻主消息
    @ApiOperation(value = "发布寻主消息",notes = "")
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){

        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            Long id = loginInfo.getId();
            searchMasterMsgService.publish(searchMasterMsg,id);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("发布失败!" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误，稍后重试!!!");
        }
    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            return searchMasterMsgService.toHandle(query,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            return searchMasterMsgService.user(query,loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 拒单接口
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id")Long id) {
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("拒单失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统错误，稍后重试!!!");
        }
    }

    /**
     * 接单接口
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public JsonResult accept(@RequestBody OrderDto dto){
        try {
            searchMasterMsgService.accept(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("接单失败");
        }
    }


}
